package compression;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.GZIPOutputStream;

public class GzipDemo {
	public static void main(String[] args) {
		String zipFile = "test.bin";
		String filename = "test.txt";
		OutputStream os = null;
		FileInputStream is = null;
		int sChunk = 8192;

		try {
			// open output Stream
			os = new FileOutputStream(zipFile);
			GZIPOutputStream zipout = new GZIPOutputStream(os);
			System.out.println("File Opened!");
			byte[] buffer = new byte[sChunk];
			try {
				is = new FileInputStream(filename);
				System.out.println("FileInputStream Open for reading!");
				int length;
				while ((length = is.read(buffer, 0, sChunk)) != -1) {
					zipout.write(buffer, 0, length);
					System.out.println(buffer);
					System.out.println(length);
				}
			} catch (IOException e) {
				// TODO: handle exception
			} finally {
				is.close();
				System.out.println("Input Stream Closed!");
			}

		} catch (IOException e) {
			// TODO: handle exception
		} finally {
			try {
				os.close();
				System.out.println("File Closed!");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
